﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Inaction.DbSchame
{
    [Serializable]
    [DisplayName("视图定义")]
    public class View : Table
    {
        public View()
        {
            Tables = new List<Table>();
        }
        string sql;
        [DisplayName("视图的Sql")]
        public string Sql
        {
            get { return sql; }
            set { sql = value; }
        }

        public string ToString()
        {
            return "View: " + base.ToString();
        }
        public List<Table> Tables
        {
            get;
            set;
        }

        List<ColumnNameAlias> columnNames = new List<ColumnNameAlias>();
        [DisplayName("列名称信息")]
        public List<ColumnNameAlias> ColumnNames
        {
            get
            {
                if (columnNames.Count == 0)
                {
                    GetColumnNames();
                }
                return columnNames;
            }
        }

        internal static Func<string, string> FormatSql;

        private void GetColumnNames()
        {
            var str = FormatSql(sql);
            str = str.Substring(str.IndexOf("SELECT", StringComparison.OrdinalIgnoreCase) + 7);
            str = str.Substring(0, str.IndexOf("FROM", StringComparison.OrdinalIgnoreCase));
            var columnSqls = str.Split(',');
            foreach (var columnName in columnSqls)
            {
                if (string.IsNullOrEmpty(columnName.Trim())) continue;
                ColumnNameAlias nameAlias = new ColumnNameAlias();
                if (columnName.Contains("."))
                {
                    nameAlias.TableName = columnName.Substring(0, columnName.IndexOf(".")).Trim();
                    nameAlias.Name = columnName.Substring(columnName.IndexOf('.') + 1).Trim();
                }
                else
                {
                    nameAlias.Name = columnName;
                }
                while (nameAlias.Name.EndsWith(" "))
                {
                    nameAlias.Name = nameAlias.Name.Substring(0, nameAlias.Name.Length - 1);
                }
                while (nameAlias.Name.StartsWith(" "))
                {
                    nameAlias.Name = nameAlias.Name.Substring(1);
                }
                if (nameAlias.Name.Contains(" "))
                {
                    nameAlias.Alias = nameAlias.Name.Substring(nameAlias.Name.LastIndexOf(' ')).Trim();
                    nameAlias.Name = nameAlias.Name.Substring(0, nameAlias.Name.IndexOf(' ')).Trim();
                }
                else
                {
                    nameAlias.Alias = nameAlias.Name;
                }
                columnNames.Add(nameAlias);
            }
        }
        [Serializable]
        public class ColumnNameAlias
        {
            [DisplayName("表名")]
            public string TableName { get; set; }
            [DisplayName("列名")]
            public string Name { get; set; }
            [DisplayName("别名")]
            public string Alias { get; set; }

            public override string ToString()
            {
                return (string.IsNullOrEmpty(TableName) ? "" : TableName + ":")
                    + Name
                    + "(" + Alias + ")";
            }
        }
    }
}
